//-------------- Data providers para recuperacion de eventos ----------------

var EBEventData = Class.create( );
EBEventData.prototype = Object.extend( new EBDataProvider( ), {
	initialize: function( ) {
		this.id = 'events';
		this.url = $RGC().eventsUrl;
		this.updateTime = $RGC().updateTime;
		this.events = new Array();
		this.markers = new Array();
		//this.startTimer();
	},
	
	setParams: function() {
		var sDateStr = new EBDateTool($RGE('browser').startDate);
		var eDateStr = new EBDateTool($RGE('browser').endDate);
		this.params = '&sd=' + sDateStr.getString() + '&ed=' + eDateStr.getString();
	},
	
	startTimer: function( ) {
		this.timer = new PeriodicalExecuter( function() { $RGE('event-data').load(); }, this.updateTime);
	},	
	
	parseXml: function( ) {
		var events = this.xml.getElementsByTagName( 'event' );
		if( events.length ) {
			$A( events ).each(
				function( el ) {
					var e = new Array();
					e['id'] = el.getAttribute( 'id' );
					e['title'] = el.getAttribute( 'label' );
					e['type'] = el.getAttribute( 'type' );
					e['priority'] = el.getAttribute( 'priority' );
					e['date'] = el.getElementsByTagName( 'date' )[ 0 ].firstChild.nodeValue;
					e['hour'] = el.getElementsByTagName( 'hour' )[ 0 ].firstChild.nodeValue;
					e['keywords'] = Try.these( 
						function() { return el.getElementsByTagName( 'keywords' )[ 0 ].firstChild.nodeValue; },
						function() { return '' }
					);
					e['description'] = Try.these( 
						function() { return el.getElementsByTagName( 'description' )[ 0 ].firstChild.nodeValue; },
						function() { return '' }
					);
					this.events.push(e);
				}.bind( this )
			);
		}
		var markers = this.xml.getElementsByTagName( 'mark' );
		if( markers.length ) {
			$A( markers ).each(
				function( el ) {
					var m = new Array();
					m['color'] = el.getAttribute( 'color' );
					m['date'] = el.getElementsByTagName( 'date' )[ 0 ].firstChild.nodeValue;
					this.markers.push(m);
				}.bind( this )
			);
		}
		return( true );
	}
}
);

var EBEventListData = Class.create( );
EBEventListData.prototype = Object.extend( new EBDataProvider( ), {
	initialize: function( ) {
		this.id = 'eventlist';
		this.url = $RGC().eventsListUrl;
		this.events = new Array();
	},
	
	parseXml: function( ) {
		var events = this.xml.getElementsByTagName( 'event' );
		if( events.length ) {
			$A( events ).each(
				function( el ) {
					var e = new Array();
					e['id'] = el.getAttribute( 'id' );
					e['title'] = el.getAttribute( 'label' );
					e['type'] = el.getAttribute( 'type' );
					e['priority'] = el.getAttribute( 'priority' );
					e['date'] = el.getElementsByTagName( 'date' )[ 0 ].firstChild.nodeValue;
					e['hour'] = el.getElementsByTagName( 'hour' )[ 0 ].firstChild.nodeValue;
					e['keywords'] = Try.these( 
						function() { return el.getElementsByTagName( 'keywords' )[ 0 ].firstChild.nodeValue; },
						function() { return '' }
					);
					e['description'] = Try.these( 
						function() { return el.getElementsByTagName( 'description' )[ 0 ].firstChild.nodeValue; },
						function() { return '' }
					);
					this.events.push(e);
				}.bind( this )
			);
		}
		return( true );
	}
}
);

var EBEventDayData = Class.create( );
EBEventDayData.prototype = Object.extend( new EBDataProvider( ), {
	initialize: function( ) {
		this.id = 'eventdays';
		this.url = $RGC().globalEventsUrl;
		this.updateTime = $RGC().eventDaysUpdateTime;
		this.dates = new Array();
		//this.startTimer();
	},
	
	setParams: function() {
		var sDateStr = new EBDateTool($RGE('days').startDate);
		var eDateStr = new EBDateTool($RGE('days').endDate);
		this.params = 'p=' + $RGE('priority').level + '&sd=' + sDateStr.getString() + '&ed=' + eDateStr.getString();
	},
	
	startTimer: function( ) {
		this.timer = new PeriodicalExecuter( function() { $RGE('days-data').preload(); }, this.updateTime);
	},
	
	preload: function( ) {
		$RGE('days').clear();
		this.setParams();
		this.load();
	},
	
	parseXml: function( ) {
		var events = this.xml.getElementsByTagName( 'event' );
		if( ! events.length ) return( false );
		$A( events ).each(
			function( el ) {
				var d = new Array();
				d['priority'] = el.getAttribute( 'priority' );
				var date = el.getElementsByTagName( 'date' )[ 0 ].firstChild.nodeValue; // -- dd/mm/yyyy --
				var parts = date.split('/');
				d['date'] = new Date(parts[2], parseInt(parts[1])-1, parts[0]);
				this.dates.push(d);
				//$RGE('days').setDate();
			}.bind( this )
		);
		return( true );
	}
}
);

var EBUndefinedEventData = Class.create( );
EBUndefinedEventData.prototype = Object.extend( new EBDataProvider( ), {
	initialize: function( ) {
		this.id = 'uevents-data';
		this.url = $RGC().undefinedEventsUrl;
		this.events = new Array();
	},
	
	parseXml: function( ) {
		var events = this.xml.getElementsByTagName( 'event' );
		if( events.length ) {
			$A( events ).each(
				function( el ) {
					var e = new Array();
					e['id'] = el.getAttribute( 'id' );
					e['title'] = el.getAttribute( 'label' );
					e['type'] = el.getAttribute( 'type' );
					e['priority'] = el.getAttribute( 'priority' );
					e['keywords'] = Try.these( 
						function() { return el.getElementsByTagName( 'keywords' )[ 0 ].firstChild.nodeValue; },
						function() { return '' }
					);
					e['description'] = Try.these( 
						function() { return el.getElementsByTagName( 'description' )[ 0 ].firstChild.nodeValue; },
						function() { return '' }
					);
					this.events.push(e);
				}.bind( this )
			);
		}
		return( true );
	}
}
);
